setClass(
    Class = "neurosdist",
    representation = representation(
        nsubs = "numeric",
        nvoxs = "numeric"
    ),
    contains = "nifticor"
)

# kendall's W 

## input is cormat
## gather matrix with voxel maps for each participant
## compute kendall's W

kendall.nifticor <- function(xs, m=NULL, verbose=TRUE, ...) {
    require(irr)

    xs <- getnifticors(xs, m)
    nvoxs <- length(xs[[1]])
    
    if (verbose) {
        pb = create.progressbar(nvoxs)
        update.progressbar(pb, 0)
    }
    
    kfun <- function(v) {
        if (verbose) update.progressbar(pb, v)
        voxmaps <- sapply(xs, function(x) x[,v])
        return(kendall(voxmaps, ...)$statistic)
    }
    
    if (getOption("allowParallel") && getDoParRegistered())
        ks <- foreach(v=1:nvoxs) %dopar% kfun(v)
    else
        ks <- sapply(1:nvoxs, kfun)

    if (verbose) end.progressbar(pb)

    return(ks)
}

# REHO



##

# homotopic

###
# Subject Correlation Map Distances
###

neurosdist <- function(xs, m=NULL, backingprefix=NULL, verbose=T, overwrite=F, ...) {
    xs <- getnifticors(xs)
    m <- getmask(m)
    
    nsubs <- length(xs)
    nvoxs <- ncol(xs[[1]])
    
    if (verbose) {
        pb = create.progressbar(nvoxs)
        update.progressbar(pb, 0)
    }
    
    # big matrix
    if (is.null(backingprefix)) {
        bigmat <- big.matrix(nsubs^2, nvoxs, init=0)
        descriptorfile=""
    } else {
        backingprefix <- rmext(abspath(backingprefix))
        backingpath <- dirname(backingprefix)
        backingfile <- sprintf("%s.bin", basename(backingprefix))
        descriptorfile <- sprintf("%s.desc", basename(backingprefix))
        neurosdistfile <- sprintf("%s.neurosdist", basename(backingprefix))
        
        if (file.exists(file.path(backingpath, backingfile))) {
            warning("distance map already exist")
            if (overwrite == FALSE) {
                message("returning previous distance map")
                return(attach.neurosdist(backingprefix))
            }
        }
        
        bigmat <- big.matrix(nsubs^2, nvoxs, init=0, backingpath=backingpath, backingfile=backingfile, descriptorfile=descriptorfile)
        dput(list(header=xs[[1]]@header, mask_used=xs[[1]]@mask_used, descriptorfile=descriptorfile, nsubs=nsubs, nvoxs=nvoxs), file.path(backingpath, neurosdistfile))
    }
    
    dmat <- new("nifticor", address=tmp@address, header=xs[[1]]@header, mask_used=xs[[1]]@mask_used, descriptorfile=descriptorfile, nsubs=nsubs, nvoxs=nvoxs)
    
    dfun <- function(v) {
        if (verbose) update.progressbar(pb, v)
        voxmaps <- sapply(xs, function(x) x[,v])
        dmat[,v] <- as.vector(1 - cor(voxmaps, ...))
    }
    
    if (getOption("allowParallel") && getDoParRegistered())
        foreach(v=1:nvoxs) %dopar% dfun(v)
    else
        lapply(1:nvoxs, dfun)
 
    if (verbose) end.progressbar(pb)
    
    return(dmat)
}

attach.neurosdist <- function(backingprefix) {
    # Setup Paths
    backingprefix <- rmext(abspath(backingprefix))
    descriptorfile <- sprintf("%s/%s.desc", dirname(backingprefix), basename(backingprefix))
    neurosdistfile <- sprintf("%s/%s.neurosdist", dirname(backingprefix), basename(backingprefix))
    
    bigmat <- attach.big.matrix(descriptorfile)
    nlist <- dget(neurosdistfile)
    
    return(new("neurosdist", address=bigmat@address, header=nlist$header, mask_used=nlist$header, descriptorfile=descriptorfile, nsubs=nlist$nsubs, nvoxs=nlist$nvoxs))
}
